package com.zxl.manager.privacy.utils.utils;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import com.zxl.manager.privacy.utils.ApplicationHelper;

import java.lang.reflect.Method;

/**
 * 和绘制相关的信息接口
 * Created by makai on 15-6-8.
 */
public class DrawUtils {

	public static float sDensity = 1.0F;
	public static int sDensityDpi;
	public static float sFontDensity;
	public static int sWidthPixels = -1;
	public static int sHeightPixels = -1;
	public static int sRealWidthPixels = -1;
	public static int sRealHeightPixels = -1;
	private static Class<?> sClass = null;
	private static Method sMethodForWidth = null;
	private static Method sMethodForHeight = null;
	public static float sVirtualDensity = -1.0F;
	public static float sVirtualDensityDpi = -1.0F;
	public static int sTouchSlop;

	public static synchronized void resetDensity(Context context) {
		if(context != null && null != context.getResources()) {
			DisplayMetrics metrics = context.getResources().getDisplayMetrics();
			sDensity = metrics.density;
			sFontDensity = metrics.scaledDensity;
			sDensityDpi = metrics.densityDpi;
			WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
			Display display = wm.getDefaultDisplay();

			try {
				sWidthPixels = display.getWidth();
				sHeightPixels = display.getHeight();
				Class e = Class.forName("android.view.Display");
				Point realSize = new Point();
				Method method = e.getMethod("getRealSize", new Class[]{Point.class});
				method.invoke(display, new Object[]{realSize});
				sRealWidthPixels = realSize.x;
				sRealHeightPixels = realSize.y;
			} catch (Throwable var8) {
				var8.printStackTrace();
				sRealWidthPixels = sWidthPixels;
				sRealHeightPixels = sHeightPixels;
			}

			try {
				ViewConfiguration e1 = ViewConfiguration.get(context);
				if(null != e1) {
					sTouchSlop = e1.getScaledTouchSlop();
				}
			} catch (Throwable var7) {
				var7.printStackTrace();
			}
		}

	}


	public static int dip2px(float dpValue) {
		final float scale = ApplicationHelper.getContext().getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static int px2dip(float pxValue) {
		final float scale = ApplicationHelper.getContext().getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public static int px2sp(float pxValue, float fontScale) {
		return (int) (pxValue / fontScale + 0.5f);
	}

	public static int sp2px(float spValue, float fontScale) {
		return (int) (spValue * fontScale + 0.5f);
	}

	public static int getScreenWidth() {
		DisplayMetrics metrics = ApplicationHelper.getContext().getResources().getDisplayMetrics();
		return metrics.widthPixels;
	}

	public static int getScreenHeight() {
		DisplayMetrics metrics = ApplicationHelper.getContext().getResources().getDisplayMetrics();
		return metrics.heightPixels;
	}

}
